﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Hospital_BackgoodsList : System.Web.UI.Page
{
    string BackPlanId, strSql, strConditions;
    public int AccountID_yy;
    public string returnstate = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        BackPlanId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["BackPlanId"]));
        //  projectid = SysFun.ToTrim(Request.QueryString["projectid"]); 2007/08/21 

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            AccountID_yy = SysFun.ToInt(_Account.AccountId);
            LabHospital.Text = _Account.DWName;
            if (!IsPostBack)
            {
                Db db = new Db();
                System.Data.IDataReader dr;
                strSql = "select * from BackPlan where BackPlanId=" + BackPlanId + " and AccountId_YY=" + _Account.AccountId;
                dr = db.DataReader(strSql);
                if (dr.Read())
                {
                    txtPlanName.Text = SysFun.ToTrim(dr["PlanName"]);
                }
                else
                {
                    Response.Write("对不起，你没有权限浏览不属于自身采购单的药品");
                    dr.Close();
                    dr.Dispose();
                    db.Close();
                    return;
                }
                dr.Close();
                dr.Dispose();
                db.Close();

            }

            if (Request.Form["btnSubmit"] == "确定选取" && Request.Form["chk"] != null)
            {
                int rowCun = 0;
                Db db = new Db();
                string BuyPlanGoodsId = Request.Form["chk"].ToString();
                strSql = "select count(BuyPlanGoodsId) as cun from BackPlanGoods where  buyplanGoodsId in (" + BuyPlanGoodsId + ") and  BackPlanId=" + BackPlanId;
                System.Data.IDataReader dr;
                dr = db.DataReader(strSql);
                if (dr.Read())
                {
                    rowCun = SysFun.ToInt(dr["cun"]);
                }
                dr.Close();
                dr.Dispose();

                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('所选药品已经在该退货单中存在，不能添加！！！');</script>");
                }
                else
                {
                    string[] SGID = BuyPlanGoodsId.Length == 0 ? new string[] { "" } : BuyPlanGoodsId.Substring(0).Split(new char[] { ',' });

                    strSql = "";
                    string _strAccountId_PS, buyplanid;
                    for (int i = 0; i <= SGID.Length - 1; i++)
                    {
                        _strAccountId_PS = SysFun.ToTrim(Request.Form["AccountId_PS" + SGID[i]]);
                        //buyplanid = SysFun.ToTrim(Request.Form["buyplan" + SGID[i]]);
                        strSql += "@" + "Insert Into BackPlanGoods(BuyPlanGoodsId,BackPlanId,AccountId_PS) values(" +
                            SGID[i] + "," + BackPlanId + "," + _strAccountId_PS + ")";
                        //设置所选药品的退货状态为  退货中
                        strSql += "@" + "update buyplangoods set ReturnState =1 where BuyPlanGoodsId =" + SGID[i];
                    }
                    // Response.Write(strSql);
                    if (strSql.Length > 0)
                    {
                        strSql += "@" + "update backPlan set BuyStatus='1' where BackPlanId=" + BackPlanId;

                        db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入,更新", "BackPlanGoods,backplan", strSql, "../Hospital/BackgoodsList.aspx");
                    }
                    db.Close();
                    Response.Write("<script language=JavaScript>window.alert('选取成功');</script>");

                    //关闭窗口，ie6-7-8不提示
                    Response.Write("<script language=JavaScript>window.open('','_top');window.top.close();</script>");
                    //刷新你窗口

                    Response.Write("<script language=JavaScript>window.opener.location.reload();</script>");
                }
                db.Close();

            }
            //if (!this.IsPostBack)
            //{
            showGridView();
            //}
        }
    }

    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "BuyGoodsView_200708";                                       //要查询的表明
        dbPage.Key = "BuyPlanGoodsId";                                              //表的主键    
        dbPage.Where = strConditions;                                               //查询条件    
        dbPage.SelectField = " GoodsId,UserCode_SC,UserCode_PS,BuyPlanGoodsId,buyplanid,SupplyGoodsId,GoodsId,AccountId_PS,InflowNumStock,"
                            + " case isnull(ReturnState,0) when 0 then '未指定退货' when 1 then '退货中' when 2 then '已认定退货' end as ReturnState,"
                            + " Case DealStatus When '0' Then '未确认送货' WHEN '1' Then '确认送货' WHEN '2' then '确认入库' WHEN '3' then '退货' END AS DealStatus,"
                            + " ProductCode,ProductName,MedicineModel,Outlookc,ProjectId,Factor,CompanyName_SC,CompanyName_TB,CompanyName_PS,TradePrice,"
                            + " Case GoodsState When '0' Then '原挂网' WHEN '1' Then '备案采购商品' WHEN '2' then '不限价不竞价商品' END AS GoodsState,"
                            + " GoodsName,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,"
                            + " case QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + "  END AS QualityLevels,"
                            + " batchnumber,validityDate";                          //要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);             //要显示第几页   
        dbPage.PageSize = 10;                                                       //没有显示记录条数   
        dbPage.OrderBy = "ProductName,BuyPlanGoodsId";                              //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        DlAttorney.DataSource = dr;


        DlAttorney.DataBind();

        //DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键


    }

    public string fctConditions()
    {
        string _strtemp = "", _strSelectValue = "";
        _strSelectValue = DropDownList1.SelectedValue;
        // _strtemp = " projectid=" + projectid + " and AccountId_YY=" + AccountID_yy + " and dealstatus='2' and";
        // 2007/08/22 马

        _strtemp = " AccountId_YY=" + AccountID_yy + " and dealstatus='2' and";

        String _strGoodid, _strProductName, _strUserCode_SC, _strCompanyName_SC, _strUserCode_PS, _strCompanyName_PS;



        //通用名查询

        _strProductName = TextProductName.Text.ToString().Trim();
        if (_strProductName != "")
        {
            _strtemp = _strtemp + " (ProductName like '%" + _strProductName + "%' or Product_SpellingCode like '%" + _strProductName + "%') and";
        }
        //商品流水号查询

        _strGoodid = TextGoodsId.Text.ToString().Trim();
        if (_strGoodid != "")
        {
            _strtemp = _strtemp + " Goodsid = '" + _strGoodid + "' and";
        }
        //生产企业查询
        _strCompanyName_SC = TextCompanyName_SC.Text.ToString().Trim();
        if (_strCompanyName_SC != "")
        {
            _strtemp = _strtemp + " CompanyName_SC like '%" + _strCompanyName_SC + "%' and";
        }
        //生产企业编码查询
        _strUserCode_SC = TextUserCode_SC.Text.ToString().Trim();
        if (_strUserCode_SC != "")
        {
            _strtemp = _strtemp + " UserCode_SC like '%" + _strUserCode_SC + "%' and";
        }
        //配送企业查询

        _strCompanyName_PS = TextCompanyName_PS.Text.ToString().Trim();
        if (_strCompanyName_PS != "")
        {
            _strtemp = _strtemp + " CompanyName_PS like '%" + _strCompanyName_PS + "%' and";
        }
        //配送企业编码查询

        _strUserCode_PS = TextUserCode_PS.Text.ToString().Trim();
        if (_strUserCode_PS != "")
        {
            _strtemp = _strtemp + " UserCode_PS like '%" + _strUserCode_PS + "%' and";
        }
        //目录范围查询　１为未退货的已购药品　２为所有已购药品

        if (_strSelectValue == "1")
        {
            _strtemp = _strtemp + " BuyPlanGoodsId not in (select BuyPlanGoodsId from BackPlanGoods where  BackPlanId=" + BackPlanId + ") and";
        }
        if (_strtemp != "")
        {
            _strtemp = "  " + _strtemp.Substring(0, _strtemp.Length - 3);
        }

        return _strtemp;
    }

}
